package com.ukao.steward.printer;

import android.os.RemoteException;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.ukao.steward.bean.ReceiptBean;
import com.ukao.steward.bean.StringBean;
import com.ukao.steward.util.CheckUtils;
import com.ukao.steward.util.DecimalUtil;
import com.ukao.steward.util.MyDateUtils;

import java.util.ArrayList;
import java.util.List;

import woyou.aidlservice.jiuiv5.IWoyouService;

import static com.ukao.steward.R.id.price;
import static com.ukao.steward.R.id.totalAmount;
import static com.ukao.steward.ui.main.MainActivity.woyouService;


/**
 *打印小米的小票
 */

public    class PrintMiuiHelper {


    public   static   final   int   ALIGMENT_RIGHT=0;
    public   static   final   int   ALIGMENT_CENTER=1;
    public   static   final   int   ALIGMENT_LEFT=2;

    /**
     * 打印小票
     * @param woyouService
     * @param printInfo
     */
    public static   void receiptReceipt( IWoyouService woyouService, ReceiptBean printInfo){
        try {


            /*/*店名  与单号*/
            woyouService.setFontSize(40,null);/*字体大小*/
            woyouService.setAlignment(PrintMiuiHelper.ALIGMENT_CENTER,null); /*对其方式*/
            if (!CheckUtils.isEmpty(printInfo.getStoreName())){
                woyouService.printText(printInfo.getStoreName()+"\n",null);/*店名*/
            }
            woyouService.setFontSize(25,null);/*字体大小*/
            woyouService.printText(printInfo.getOrderNo()+"\n"+"\n",null);/*单号*/
            /*设置二维码*/
            woyouService.printQRCode(printInfo.getOrderNo(),5,2,null);/*二维码*/
           // woyouService.printBarCode(printInfo.getOrderNo(),8,115,2,0,null);/*128条码*/

            List<ReceiptBean.OrderProductListBean> mProductList=printInfo.getOrderProductList();
            List<StringBean> mClothlist = null;
            if (!CheckUtils.isEmpty(printInfo.getProPriceDesc())){
                Gson mGson = new Gson();
          mClothlist = mGson.fromJson(printInfo.getProPriceDesc(),
                        new TypeToken<List<StringBean>>() {
                        }.getType());
            }

            /*入库*/
            if (!CheckUtils.isEmpty(printInfo.getRecoredTime())){
                woyouService.lineWrap(1,null);

                String[] storagtext = new String[2];
                storagtext[0]="入库："+ MyDateUtils.formatDateCustom(printInfo.getRecoredTime(),"MM-dd HH:mm:ss");
                String    cloCount=printInfo.getClothCnt()+printInfo.getAnnexCnt();
                storagtext[1]="件数："+ printInfo.getProductCnt()+"件";
                woyouService.printText(storagtext[0]+"\n",null);
                woyouService.printText(cloCount+"\n",null);
             //   int[] widthstory = new int[] { 20, 10};
               // woyouService.printColumnsText(storagtext, widthstory, new int[] { 0, 2 },null);

            }
            woyouService.printText(PrintMiuiHelper.cutoffs(),null);
            woyouService.setAlignment(PrintMiuiHelper.ALIGMENT_RIGHT,null); /*对其方式*/


            /*衣物列表*/
            if (!CheckUtils.isEmpty(mProductList)) {

                woyouService.printText("入库衣物清单"+"\n",null);
                Gson mGson = new Gson();
                for (int j = 0; j < mProductList.size(); j++) {

                    ReceiptBean.OrderProductListBean orderProductListBean = mProductList.get(j);
                    String color = "";
                    if (!CheckUtils.isEmpty(orderProductListBean.getColorDesc())) {  /*/*瑕疵*/
                        List<StringBean> mColorDesclist = mGson.fromJson(orderProductListBean.getColorDesc(),
                                new TypeToken<List<StringBean>>() {
                                }.getType());
                        StringBuffer mColorBuffer1 = new StringBuffer();
                        for (int i = 0; i < mColorDesclist.size(); i++) {
                            if (i != mColorDesclist.size() - 1) {
                                mColorBuffer1.append(mColorDesclist.get(i).getName() + "/");
                            } else {
                                mColorBuffer1.append(mColorDesclist.get(i).getName());
                            }


                        }
                        color = mColorBuffer1.toString();
                    }

                    String strflawDesc = "";
                    if (!CheckUtils.isEmpty(orderProductListBean.getFlawDesc())) {  /*/*瑕疵*/
                        List<StringBean> mflawDesclist = mGson.fromJson(orderProductListBean.getFlawDesc(),
                                new TypeToken<List<StringBean>>() {
                                }.getType());
                        StringBuffer mflawBuffer = new StringBuffer();
                        for (int i = 0; i < mflawDesclist.size(); i++) {
                            if (i != mflawDesclist.size() - 1) {
                                mflawBuffer.append(mflawDesclist.get(i).getName() + "/");
                            } else {
                                mflawBuffer.append(mflawDesclist.get(i).getName());
                            }


                        }
                        strflawDesc = mflawBuffer.toString();
                    }



                   String    productName=orderProductListBean.getProductName() + " " + orderProductListBean.getScanCode();
                    woyouService.printText(productName+"\n",null);


                    String[] piceText = new String[2];
                    piceText[0]="￥" + CheckUtils.isEmptyNumber(orderProductListBean.getServiceAmt()) + "*" + orderProductListBean.getDiscount() + "%" ;
                    piceText[1]="￥"+CheckUtils.isEmptyNumber(orderProductListBean.getSubtotal());
                    int[] widthPice = new int[] { 20, 10};
                    woyouService.printColumnsText(piceText, widthPice, new int[] { 0, 2 },null);


                    if (!CheckUtils.isEmpty(color)&&!CheckUtils.isEmpty(strflawDesc)){
                        color=color+"/"+strflawDesc;
                     }else if (CheckUtils.isEmpty(color)&&!CheckUtils.isEmpty(strflawDesc)){
                        color=strflawDesc;
                    }

                    if (!color.isEmpty()){
                        woyouService.printText(color+"\n",null);
                    }


                }
            }
            String  orderTotalAmount = "0.00";
            if (!CheckUtils.isEmpty(mClothlist)&&CheckUtils.isEmpty(mProductList)){
                woyouService.printText("下单衣物清单:"+"\n",null);
                for (StringBean stringBean : mClothlist) {
                    String    productName=stringBean.getName() ;
                    woyouService.printText(productName+"\n",null);
                    String[] piceText = new String[2];
                    String discount = stringBean.getDiscount() == 0 || stringBean.getDiscount() == 100 ? "" : "*"+stringBean.getDiscount() + "%";
                    String unitPrice = CheckUtils.isEmptyNumber(stringBean.getPrice());
                    String cnts=stringBean.getCnts();
                    String totalAmount ="";
                    if (!CheckUtils.isEmpty(discount)){
                         totalAmount = DecimalUtil.multiply(cnts, DecimalUtil.multiply(unitPrice, CheckUtils.isEmptyNumber(stringBean.getDiscount())));

                    }else{
                        totalAmount=DecimalUtil.multiply(cnts,unitPrice);
                    }
                    orderTotalAmount= DecimalUtil.add(orderTotalAmount, totalAmount);
                    piceText[0]="￥" +unitPrice;
                    piceText[1]="￥"+totalAmount;
                    int[] widthPice = new int[] { 20, 10};
                    woyouService.printColumnsText(piceText, widthPice, new int[] { 0, 2 },null);

                }





            }


            if (!CheckUtils.isEmpty(mProductList)||!CheckUtils.isEmpty(mClothlist)){
                woyouService.printText(PrintMiuiHelper.cutoffs(),null);
            }



            /*付款状态与价格*/
            woyouService.setAlignment(PrintMiuiHelper.ALIGMENT_LEFT,null); /*对其方式*/
            String cloTotalAmount ;
                if (!CheckUtils.isNull(printInfo.getValuationPrice()) ||CheckUtils.isEmptyInterExist(printInfo.getOrderPrice()) ||!CheckUtils.isNull(printInfo.getModifyPrice())) {  //下单模式 1.预约模式 2.下单支付模式
                /*计价*/
                if (!CheckUtils.isNull(printInfo.getValuationPrice())) {
                    cloTotalAmount=CheckUtils.isEmptyNumber(printInfo.getValuationPrice());
                } else {
                    int  modifyPrice=CheckUtils.isNull(printInfo.getModifyPrice())?0:printInfo.getModifyPrice();
                    int orderPrice = CheckUtils.isNull(printInfo.getOrderPrice()) ? 0 : printInfo.getOrderPrice();
                    int totalPrice = orderPrice != 0 ? printInfo.getOrderPrice() : modifyPrice;
                    cloTotalAmount = CheckUtils.isEmptyNumber(totalPrice);

                }


            } else {
                cloTotalAmount=orderTotalAmount;
            }

             woyouService.printText("衣物总额："+cloTotalAmount+"\n",null);/*单号*/
            if (printInfo.getCouponAmt()!=0){/*优惠卷 */
                woyouService.printText("优惠卷："+CheckUtils.isEmptyNumber(printInfo.getCouponAmt())+"\n",null);

            }
            if (printInfo.getIsModifyPrice()){/*修改金额 */
                woyouService.printText("修改金额："+CheckUtils.isEmptyNumber(printInfo.getModifyPrice())+"\n",null);

            }
            int   takesEndFee=printInfo.getSendCarriage()+printInfo.getTakeCarriage();
            if (takesEndFee!=0){  /*取送费*/
                woyouService.printText("取送费："+CheckUtils.isEmptyNumber(takesEndFee)+"\n",null);
            }


            if (printInfo.getPayStatus()){/* 已支付金额*/
                woyouService.printText("已付金额："+CheckUtils.isEmptyNumber(printInfo.getPaidPrice())+"\n",null);
            }
            woyouService.printText("付款状态："+printInfo.getPayStatusText()+"\n",null);
            woyouService.setAlignment(PrintMiuiHelper.ALIGMENT_RIGHT,null); /*对其方式*/
            woyouService.printText(PrintMiuiHelper.cutoffs(),null);

            /*客户备注*/
            if (!CheckUtils.isEmpty( printInfo.getUserRemark())){
                woyouService.printText("客户备注："+printInfo.getUserRemark()+"\n",null);
            }else{
                woyouService.printText("客户备注：无"+"\n",null);
            }
            woyouService.printText(PrintMiuiHelper.cutoffs(),null);
            String  userName="";
            if (!CheckUtils.isEmpty(printInfo.getUserName())){
                userName="  "+printInfo.getUserName();
            }
            if (!CheckUtils.isEmpty(printInfo.getUserPhone())){
                userName=printInfo.getUserPhone()+userName;
            }
            woyouService.printText("客户："+userName+"\n",null);
            woyouService.printText("卡号："+printInfo.getUserCardNo()+"\n",null);
            String  discount="";
            if (printInfo.getDiscountType()){
                discount="多折扣";
            }else{
                discount=printInfo.getDiscount()+"%";
            }
            woyouService.printText("折扣："+discount+"%"+"  余额：￥"+CheckUtils.isEmptyNumber(printInfo.getUserBalance())+"\n",null);
            if (!CheckUtils.isEmpty(printInfo.getTakeAddress())){
                woyouService.printText(printInfo.getTakeAddress()+"\n",null);
            }
            woyouService.printText(PrintMiuiHelper.cutoffs(),null);
            if (!CheckUtils.isEmpty(printInfo.getWxCode())){
                woyouService.printText("微信号："+printInfo.getWxCode()+"\n",null);
            }
            if (!CheckUtils.isEmpty(printInfo.getServiceTel())){
                woyouService.printText("客服电话："+printInfo.getServiceTel()+"\n",null);
            }
            woyouService.printText("打印时间："+MyDateUtils.getDateTime()+"\n"+"\n",null);
            woyouService.lineWrap(5,null);
            woyouService.commitPrinterBuffer();





        }catch (Exception e){

        }


    }


    public static   String cutoffs(){
        StringBuffer  mStringBuffer=new StringBuffer();
        String  cutline= "_____________________________";

        return mStringBuffer.append(cutline).toString()+"\n";
    }





}
